package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.cache.impl.KiteContextCache;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.cust.CuccContextCustDTO;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalQuerySeqHelper;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.GeneralApnTypeAttrHandler;
import com.iwhalecloud.bss.kite.cucc.service.util.ProdCateUtil;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.iwhalecloud.bss.pub.client.constanst.Symbols;
import com.ztesoft.bss.common.util.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 通用连接APN类型联动设值
 * Created by  huanghaosheng on 2020/11/25 14:53
 */
@Service
@AttrHandler(GeneralApnTypeAttrHandler.class)
public class GeneralApnTypeAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    OperateInstAttrService operateInstAttrService;

    @Autowired
    private IQuerySceneInstService querySceneInstService;


    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        KiteAttr apnIdentify = inst.getAttr("200004104");
        if (Objects.isNull(apnIdentify) || KiteStringUtils.isEmpty(attr.getValue())) {
            return;
        }
        KiteAttr apnName = inst.getAttr("200002833");
        KiteAttr accAttr = querySceneInstService
            .getCommonAttrsByFieldName(data.getSceneInstId(), "business_number", "113");
        if (Objects.nonNull(accAttr)) {
            List<KiteAttrValue> attrValues = new ArrayList<>();
            if (KiteStringUtils.equals("100007282", attr.getValue())) {
                String attrId = apnIdentify.getAttrId();
                List<AttrValue> values = AttrValueCache.get(attrId);
                values.stream().forEach(attrValue ->{
                    KiteAttrValue kiteAttrValue = new KiteAttrValue();
                    kiteAttrValue.setAttrValue(attrValue.getAttrValue());
                    kiteAttrValue.setAttrValueName(attrValue.getAttrValueName());
                    attrValues.add(kiteAttrValue);
                });
                apnIdentify.setAttrValues(attrValues);
                apnIdentify.setIsEdit("T");
                operateInstAttrService.setAttrValue(inst, "200004104", apnIdentify.getDefaultValue(), data.getSceneInstId(), null);
                operateInstAttrService.setAttrValue(inst, "200002833", apnIdentify.getValueDesc(), data.getSceneInstId(), null);
                // FORM表单的展示，融合，4G通用连接通信计划APN 和APN标识一致，前端需要进行重刷
                if (KiteStringUtils.equalsAny(inst.getSpecId(), ProdCateUtil.getMainMemberProdIdForArr(ProdCateUtil.GIFT_TYCAT1_PROD, ProdCateUtil.TY4G_PROD, ProdCateUtil.TYCAT1_PROD))) {
                    operateInstAttrService.setAttrValue(inst, "200004078", apnIdentify.getValueDesc(), data.getSceneInstId(), null);
                }
            }
            else {
                String name = accAttr.getValue();
                CuccContextCustDTO custDTO = KiteContextCache.get(KiteStringUtils.join(ContextUtil.getHttpSessoinId(), Symbols.HASH, data.getCustId(), Symbols.AT, "custInfo"));
                Map<String, String> inParam = new HashMap<>();
                inParam.put("SEQ_TYPE", "WLW_APN_ID");
                inParam.put("SYS_TYPE", "1");
                inParam.put("SYS_SOURCE", "");
                String seq = LocalQuerySeqHelper.getSeqBySeqType(inParam, data.getSceneInstId(), custDTO);
                name = name + "_APN_" + seq;
                KiteAttrValue attrValue = new KiteAttrValue();
                attrValue.setAttrValue(name);
                attrValue.setAttrValueName(name);
                attrValues.add(attrValue);
                apnIdentify.setAttrValues(attrValues);
                apnIdentify.setIsEdit("F");
                operateInstAttrService.setAttrValue(inst, "200004104", name, data.getSceneInstId(), null);
                operateInstAttrService.setAttrValue(inst, "200002833", "待生成", data.getSceneInstId(), null);
                // FORM表单的展示，融合，4G通用连接通信计划APN 和APN标识需要一致，前端需要进行重刷
                if (KiteStringUtils.equalsAny(inst.getSpecId(), ProdCateUtil.getMainMemberProdIdForArr(ProdCateUtil.GIFT_TYCAT1_PROD, ProdCateUtil.TY4G_PROD, ProdCateUtil.TYCAT1_PROD))) {
                    operateInstAttrService.setAttrValue(inst, "200004078", name, data.getSceneInstId(), null);
                }
            }
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {
        if (KiteStringUtils.equals(inst.getSpecId(), ProdCateUtil.getMainMemberProdId(ProdCateUtil.GIFT_TYCAT1_PROD))) {
            if (KiteStringUtils.equals(attr.getValue(), "100007282")) {
                LocalAttrHelper.hideAndNullable(inst, "200001489");
                LocalAttrHelper.hideAndNullable(inst, "200004002");
            } else {
                LocalAttrHelper.showForDAndNull(inst, "200001489");
                LocalAttrHelper.showForDAndNull(inst, "200004002");
            }
        }
    }
}
